
package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.ScreenRecordAdapter;
import flc.ast.bean.ScreenRecordBean;
import flc.ast.databinding.DialogScreenRecordBinding;
import stark.common.basic.base.BaseSmartDialog;

public class ScreenRecordDialog extends BaseSmartDialog<DialogScreenRecordBinding> implements View.OnClickListener {

    private ScreenRecordAdapter recordAdapter;
    private int mCurrentPos = 1;
    private int type;


    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void setmCurrentPos(int mCurrentPos) {
        this.mCurrentPos = mCurrentPos;
    }

    public ScreenRecordDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_screen_record;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        recordAdapter = new ScreenRecordAdapter();
        mDataBinding.rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mDataBinding.rvList.setAdapter(recordAdapter);
        getDialogData();
        recordAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                recordAdapter.getItem(mCurrentPos).setSelect(false);
                recordAdapter.getItem(position).setSelect(true);
                recordAdapter.notifyDataSetChanged();
                mCurrentPos = position;
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) { //取消
            dismiss();
        } else if (id == R.id.tvConfirm) {  //确认
            if (listener != null) {
                listener.clickItem(mCurrentPos, recordAdapter.getItem(mCurrentPos).getName());
            }
            dismiss();
        }
    }


    private void getDialogData() {
        switch (type) {
            case 0:
                getResolutionData();
                break;
            case 1:
                getVoiceData();
                break;
            case 2:
                getOrientationData();
                break;
            case 3:
                getFloatData();
                break;
            default:
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        //点击item
        void clickItem(int position, String name);
    }

    private void getResolutionData() {
        String[] str = getContext().getResources().getStringArray(R.array.qxd_arr);
        List<ScreenRecordBean> list = new ArrayList<>();
        for (String s : str) {
            list.add(new ScreenRecordBean(s, false));
        }
        list.get(mCurrentPos).setSelect(true);
        recordAdapter.setList(list);
        mDataBinding.tvTitle.setText(getContext().getString(R.string.qxd_text));
    }

    private void getVoiceData() {
        String[] str = getContext().getResources().getStringArray(R.array.lzsy_arr);
        List<ScreenRecordBean> list = new ArrayList<>();
        for (String s : str) {
            list.add(new ScreenRecordBean(s, false));
        }
        list.get(mCurrentPos).setSelect(true);
        recordAdapter.setList(list);
        mDataBinding.tvTitle.setText(getContext().getString(R.string.lzsysz_text));
    }

    private void getOrientationData() {
        String[] str = getContext().getResources().getStringArray(R.array.lpfx_arr);
        List<ScreenRecordBean> list = new ArrayList<>();
        for (String s : str) {
            list.add(new ScreenRecordBean(s, false));
        }
        list.get(mCurrentPos).setSelect(true);
        recordAdapter.setList(list);
        mDataBinding.tvTitle.setText(getContext().getString(R.string.lpfx_text));
    }

    private void getFloatData() {
        String[] str = getContext().getResources().getStringArray(R.array.lpxfc_arr);
        List<ScreenRecordBean> list = new ArrayList<>();
        for (String s : str) {
            list.add(new ScreenRecordBean(s, false));
        }
        list.get(mCurrentPos).setSelect(true);
        recordAdapter.setList(list);
        mDataBinding.tvTitle.setText(getContext().getString(R.string.lpxfc_text));
    }
}
